<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>后台管理-注册</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" th:href="@{/lib/layui-v2.5.5/css/layui.css}" href="/templates/lib/layui-v2.5.5/css/layui.css"
          media="all">

    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>
        .layui-container{
        background: #e5dbdb;
    }
    .layui-anim{
        color: #e5dbdb;
    }
    .admin-login-background{
        background: url('../../static/images/bg.jpg');
        width: 1520px;
        height: 710px;
    }</style>
<body >
<div id="container layui-anim layui-anim-upbit">

    <div class="admin-login-background">
        <br> <br>
        <div class="layui-container" style="width: 380px;height: 590px;padding-top: 80px">
            <h2 style="position: relative;left:140px;margin-bottom:15px;">注册界面</h2>
            <form class="layui-form" method="get" action="" th:action="@{/user/register}">

                <div class="layui-form-item">
                    <label class="layui-form-label">用户名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="username" required lay-verify="required" placeholder="请输入用户名"
                               lay-reqtext="用户名不能为空" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">密码</label>
                    <div class="layui-input-inline">
                        <input type="password" name="password" required lay-verify="required" placeholder="请输入密码"
                               lay-reqtext="密码不能为空"     autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <lable class="layui-form-label">邮箱</lable>
                    <div class="layui-input-inline">
                        <input type="email" placeholder="请输入邮箱" name="email" required lay-verify="required"
                               lay-reqtext="邮箱不能为空"   autocomplete="off" class="layui-input " value="">
                    </div>
                </div>
                <div class="layui-input-item">
                    <lable class="layui-form-label">手机号</lable>
                    <div class="layui-input-inline">
                        <input type="text" placeholder="请输入手机号" name="phone" required lay-verify="required"
                               lay-reqtext="手机号不能为空"  autocomplete="off" class="layui-input " value="">
                    </div>
                </div>
                <div style="height: 15px"></div>
                <div class="layui-input-item">
                    <lable class="layui-form-label">密码问题</lable>
                    <div class="layui-input-inline">
                        <input type="text" placeholder="设置一个问题" name="question" required lay-verify="required"
                               lay-reqtext="问题不能为空" autocomplete="off" class="layui-input " value="">
                    </div>
                </div>

                <div style="height: 15px"></div>
                <div class="layui-input-inline">
                    <lable class="layui-form-label">答案</lable>
                    <div class="layui-input-inline">
                        <input type="text" placeholder="问题的答案" name="answer" required lay-verify="required"
                               lay-reqtext="答案不能为空"  autocomplete="off" class="layui-input " value="">
                    </div>
                </div>
                <div style="height: 15px"></div>
                <div class="layui-form-inline">
                    <label class="layui-form-label">性别</label>
                    <div class="layui-input-inline">
                        <input type="radio" name="sex" value="男" title="男">
                        <input type="radio" name="sex" value="女" title="女" checked="checked">
                    </div>
                </div>

                <div class="layui-form-inline">
                    <div class="layui-input-block" style="margin-top: 10px">
                        <button class="layui-btn admin-button " lay-submit lay-filter="register"
                                style="position: center">注 册
                        </button>
                    </div>
                </div>
            </form>
            <p class="errorMag" style="margin-top: 20px;width: 380.4px;height: 30px;text-align: center;color: crimson;font-size:18px;" ></p>
        </div>
    </div>
</div>
<script th:src="@{/lib/layui-v2.5.5/layui.js}" src="../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>

<script>
    /*邮箱格式验证*/
    function isEmail(str) {
        return /^(\w+)(\.\w+)*@(\w+)(\.\w+)*.(\w+)$/i.test(str);
    }

    function isPhone(phone){
        return /^[1][3,4,5,7,8][0-9]{9}$/i.test(phone);
    }

    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer;

        // 注册过期的时候，跳出ifram框架
        if (top.location != self.location) top.location = self.location;

        // 进行注册操作
        form.on('submit(register)', function (data) {
            data = data.field;

            if (!isEmail(data.email)) {
                layer.msg('email格式不正确');
                return false;
            }

            if (!isPhone(data.phone)){
                layer.msg('手机号格式不正确');
                return false;
            }

            $.ajax({
                data:data,
                url:"/user/register",
                method:'POST',
                dataType:'json',
                async:false,
                cache: false,
                success:function(result){
                    //注册失败

                    if (result.status!=0){
                        layer.msg(result.msg);

                        return false;
                    }else{
                        //注册成功去登陆
                        window.location.href="/user/";
                    }
                    console.log(result);
                },
            });
            return false;
        });
    });
</script>
</body>
</html>